package com.hrt.manage.product.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.hrt.base.BaseDaoImpl;
import com.hrt.bean.ProductInfor;
import com.hrt.bean.Resources;
import com.hrt.manage.product.dao.ProInforDao;


@Repository
public class ProInforDaoImpl extends BaseDaoImpl<ProductInfor> implements ProInforDao {

	
	/**
	 * 根据条件，组装HQL进行查询
	 */
	@Override
	public List<ProductInfor> queryByCondition(List conditions) {
		String hql="select product from ProductInfor product";
		for(int i=0;i<conditions.size();i++)
		{
			Map con=(Map) conditions.get(i);
			String field=con.get("field").toString();
			String val=con.get("value").toString();
			if(i==0){
			hql=hql+" where product."+field+"="+val;
			}else{
				hql=hql+" and product."+field+"="+val;
			}
		}
			List<ProductInfor> results=this.getHibernateTemplate().find(hql);
		
		return results;
	}

	
	/**
	 * 根据条件，组装HQL进行分页查询
	 */
	@Override
	public List<ProductInfor> queryPageByCondition(List conditions,
			int pageIndex, int pageSize) {
		String hql="select product from ProductInfor product";
		Map delcon=new HashMap();
		delcon.put("field", "isdelete");
	delcon.put("value",0);
		conditions.add(delcon);
		for(int i=0;i<conditions.size();i++)
		{
			Map con=(Map) conditions.get(i);
			String field=con.get("field").toString();
			String val=con.get("value").toString();
			if(i==0){
			hql=hql+" where product."+field+"="+val;
			}else{
				hql=hql+" and product."+field+"="+val;
			}
		}
		List<ProductInfor> results= queryByPage(hql, pageIndex, pageSize);
		return results;
	}


	@Override
	public ProductInfor updateTotal(ProductInfor product) {
		try {
			this.updateEntity(product);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return this.getById(product.getId());
	}


	@Override
	public List<ProductInfor> queryProducts(String itemCondition) {
		
		String hql="select product from ProductInfor product where product.isdelete=0 and (product.proCode like '%"+itemCondition+"%'"+" or product.proName like '%"+itemCondition+"%'"+")";
		
		List<ProductInfor> results=new ArrayList<ProductInfor>();
		
		 results= getHibernateTemplate().find(hql);
		return results;
	}


	


	



}
